package com.zige.zige.activity.main;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zige.zige.R;
import com.zige.zige.activity.AboutLittlezebraActivity;
import com.zige.zige.activity.BaseActivity;
import com.zige.zige.activity.PersonalInformationActivity;
import com.zige.zige.activity.RegisterActivity;
import com.zige.zige.activity.SettingCenterActivity;
import com.zige.zige.activity.UserFeedbackActivity;
import com.zige.zige.bj_vr.BJVRVideoDetail;
import com.zige.zige.pojo.UserInfo;
import com.zige.zige.utils.SharedPreferencesUtils;
import com.zige.zige.utils.SystemUtils;
import com.zige.zige.utils.ToastUtils;
import com.zige.zige.utils.VolleyImageUtils;
import com.zige.zige.view.CircleImageView;
import com.zige.zige.view.SlidingMenu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Fei on 2016/3/7.
 */
public class MainActivity extends BaseActivity implements View.OnClickListener{
    public MediaPlayer voicePlayer = new MediaPlayer();



    /****
     * 初始化底部按钮
     */
    private Button btn_first, btn_second, btn_third;
    private View view_btn1, view_btn2, view_btn3;
    private LinearLayout lay_btn1, lay_btn2, lay_btn3;
    private HorizontalScrollView scrollView_to;

    private int textWhite;
    private int textBlack;
    private int lineBlue;
    private int lineWhite;

    private int width;
    private ViewPager viewPager;
    /**菜单*/
    private SlidingMenu mMenu;
    private CircleImageView head_img;
    private TextView tv_login;
    private ImageView image_bg,img_menu;

    private RelativeLayout lay_personinfo;
    private RelativeLayout lay_setcenter;
    private RelativeLayout lay_userfeedback;
    private LinearLayout lay_title_to;

    private ImageView image_viceo_play;//声音播放

    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        userInfo = application.getUserInfo();

        Log.e("wu", SystemUtils.getDeviceKey(MainActivity.this)+"    "+SystemUtils.getDeviceKey(MainActivity.this).length());
        initColor();
        initTab();
        findView();
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void refresh() {
        image_bg = serchViewById(R.id.image_bg);
        head_img = serchViewById(R.id.head_img_bg);
        if (application.isLogin()) {
            tv_login.setText(application.getUserInfo().childName);
            tv_login.setOnClickListener(null);
            if (userInfo.headPortraitUrl.equals("null") || userInfo.headPortraitUrl == null) {
                image_bg.setVisibility(View.VISIBLE);
                head_img.setVisibility(View.GONE);
            } else {
                image_bg.setVisibility(View.GONE);
                head_img.setVisibility(View.VISIBLE);
                VolleyImageUtils.displayImage(userInfo.headPortraitUrl, head_img, MainActivity.this);
            }
        } else {
            image_bg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    userLogin();
                }
            });
        }
    }

    private void initView(){
        mMenu = serchViewById(R.id.id_menu);
        img_menu = serchViewById(R.id.img_menu);
        tv_login = serchViewById(R.id.tv_login);
        tv_login.setOnClickListener(this);
        lay_personinfo = serchViewById(R.id.lay_personinfo);
        lay_personinfo.setOnClickListener(this);
        lay_setcenter = serchViewById(R.id.lay_setcenter);
        lay_setcenter.setOnClickListener(this);
        lay_userfeedback = serchViewById(R.id.lay_userfeedback);
        lay_userfeedback.setOnClickListener(this);
        image_viceo_play = serchViewById(R.id.image_voice_play);
        image_viceo_play.setOnClickListener(this);
        lay_title_to = serchViewById(R.id.lay_title_to);
        lay_title_to.setOnClickListener(this);
        lay_title_to.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mMenu.closeMenu();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mMenu.closeMenu();
                        break;
                    case MotionEvent.ACTION_UP:

                        mMenu.closeMenu();
                        break;


                }
                return true;
            }
        });


    }

    private void setWhiteDaybg(){
        mMenu.setBackgroundResource(R.drawable.whiteday_home_menubg);
        img_menu.setImageResource(R.drawable.whiteday_menu_bg);
    }
    private void seteveningDaybg(){
        mMenu.setBackgroundResource(R.drawable.evening_home_menu_bg);
        img_menu.setImageResource(R.drawable.evening_menu_bg);
    }

    boolean isVoicePlay;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lay_title_to:
//                if (mMenu.isOpen) {
//                    mMenu.closeMenu();
//                } else {
//                    lay_title_to.setOnClickListener(null);
//                }
                break;
            case R.id.tv_login:
                mMenu.closeMenu();
                startActivity(new Intent(MainActivity.this, BJVRVideoDetail.class));
                overridePendingTransition(R.anim.l2r_in, R.anim.l2r_out);
                break;
            case R.id.lay_personinfo:
                mMenu.closeMenu();
                if (application.isLogin()) {
                    startActivity(new Intent(MainActivity.this, PersonalInformationActivity.class));
                    overridePendingTransition(R.anim.l2r_in, R.anim.l2r_out);
                } else {
                    userLogin();

                }
                break;
            case R.id.lay_setcenter:
                mMenu.closeMenu();
                if (application.isLogin()) {
                    startActivity(new Intent(MainActivity.this, SettingCenterActivity.class));
                    overridePendingTransition(R.anim.l2r_in, R.anim.l2r_out);
                } else {
                    userLogin();
                }
                break;
            case R.id.lay_userfeedback:
                mMenu.closeMenu();
                if (application.isLogin()) {
                    startActivity(new Intent(MainActivity.this, UserFeedbackActivity.class));
                    overridePendingTransition(R.anim.l2r_in, R.anim.l2r_out);
                } else {
                    userLogin();
                }
                break;
            case R.id.image_voice_play:
                isVoicePlay = !image_viceo_play.isSelected();
                if (isVoicePlay) {
                    image_viceo_play.setImageResource(R.drawable.whiteday_bg_voice_play_no);
                    image_viceo_play.setSelected(true);
                    SharedPreferencesUtils.saveValue(MainActivity.this, SharedPreferencesUtils.isplayvoice, true);
                } else {
                    image_viceo_play.setImageResource(R.drawable.whiteday_bg_voice_play);
                    image_viceo_play.setSelected(false);
                    SharedPreferencesUtils.saveValue(MainActivity.this, SharedPreferencesUtils.isplayvoice, isVoicePlay);
                }
                break;
        }
    }
    public void toggleMenu(View view) {
        mMenu.toggle();
    }

    private void userLogin() {
        ToastUtils.showToast(MainActivity.this, "请先登录");
        startActivity(new Intent(MainActivity.this, RegisterActivity.class));
        overridePendingTransition(R.anim.l2r_in, R.anim.l2r_out);
    }
    private int day=1;
    private void initColor() {
        if(day==1){
            textWhite = getResources().getColor(R.color.m_btn_text);
            textBlack = getResources().getColor(R.color.m_btn_text_select);
            lineBlue = getResources().getColor(R.color.m_view_bg_select);
            lineWhite = getResources().getColor(R.color.line_gory);
        }else if(day==2){
            textWhite = getResources().getColor(R.color.white);
            textBlack = getResources().getColor(R.color.m_btn_text);
            lineBlue = getResources().getColor(R.color.view_bg);
            lineWhite = getResources().getColor(R.color.color_253444);
        }

    }


    /**
     * 初始化tablayout
     */
    private void initTab() {
        scrollView_to = serchViewById(R.id.scrollView_to);
        scrollView_to.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                }
            }
        });
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        width = metric.widthPixels; // 屏幕宽度（像素）;

        view_btn1 = serchViewById(R.id.view_btn1);
        view_btn2 = serchViewById(R.id.view_btn2);
        view_btn3 = serchViewById(R.id.view_btn3);

        btn_first = serchViewById(R.id.btn_first);
        btn_first.setOnClickListener(new BtnSelectListener());
        btn_second = serchViewById(R.id.btn_second);
        btn_second.setOnClickListener(new BtnSelectListener());
        btn_third = serchViewById(R.id.btn_third);
        btn_third.setOnClickListener(new BtnSelectListener());

        lay_btn1 = serchViewById(R.id.lay_btn1);
        lay_btn2 = serchViewById(R.id.lay_btn2);
        lay_btn3 = serchViewById(R.id.lay_btn3);
        ViewGroup.LayoutParams params = lay_btn1.getLayoutParams();
        params.width = width / 2;
        lay_btn2.getLayoutParams().width = width / 2;
        lay_btn3.getLayoutParams().width = width / 2;
        //setText();

        btn_first.setText("今天");
        btn_second.setText("昨天");
        btn_third.setText("前天");
    }

    private void findView(){
        viewPager = serchViewById(R.id.viewpager);
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(0
        );
    }


    /**
     * 这是变色龙
     */
    private boolean isShowed;

    class BtnSelectListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.push_right_in);
            switch (v.getId()) {
                case R.id.btn_first:
                    voicePlayer.pause();
                    SharedPreferencesUtils.saveValue(MainActivity.this,SharedPreferencesUtils.voice_loading,true);
                    changeColor(textBlack, textWhite, textWhite, lineBlue, lineWhite, lineWhite);
                    view_btn1.startAnimation(animation);
                    isShowed = false;
                    viewPager.setCurrentItem(FragmentAdapter.TAB_TODAY);
                    break;
                case R.id.btn_second:
                    voicePlayer.pause();
                    SharedPreferencesUtils.saveValue(MainActivity.this,SharedPreferencesUtils.voice_loading,true);
                    changeColor(textWhite, textBlack, textWhite, lineWhite, lineBlue, lineWhite);
                    view_btn2.startAnimation(animation);
                    isShowed = !btn_second.isSelected();
                    if (isShowed) {
                        btn_second.setSelected(true);
                    } else {
                        btn_second.setSelected(false);
                    }
                    viewPager.setCurrentItem(FragmentAdapter.TAB_YESTERDAY);
                    break;
                case R.id.btn_third:
                    voicePlayer.pause();
                    SharedPreferencesUtils.saveValue(MainActivity.this,SharedPreferencesUtils.voice_loading,true);
                    changeColor(textWhite, textWhite, textBlack, lineWhite, lineWhite, lineBlue);
                    view_btn3.startAnimation(animation);
                    isShowed = false;
                    viewPager.setCurrentItem(FragmentAdapter.TAB_BEFOREYSERDAY);
                    break;
            }
        }
    }

    private void changeColor(int text1, int text2, int text3, int line1, int line2, int line3) {
        btn_first.setTextColor(text1);
        btn_second.setTextColor(text2);
        btn_third.setTextColor(text3);
        view_btn1.setBackgroundColor(line1);
        view_btn2.setBackgroundColor(line2);
        view_btn3.setBackgroundColor(line3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mMenu.closeMenu();
                break;
            case MotionEvent.ACTION_MOVE:
                mMenu.closeMenu();
                break;
            case MotionEvent.ACTION_UP:

                mMenu.closeMenu();
                break;


        }
        return super.onTouchEvent(event);
    }

    private List<String> getDate() {
        List<String> date = new ArrayList<>();
        Calendar c = Calendar.getInstance(); // 当时的日期和时间
        int yesterday = c.get(Calendar.DAY_OF_MONTH) - 1;
        c.set(Calendar.DAY_OF_MONTH, yesterday);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd");
        String yesterdayMonth = dateFormat.format(c.getTime()).substring(5, 7);
        String yesterdayDay = dateFormat.format(c.getTime()).substring(7, 10);
        String dateYesterday;
        if (Integer.parseInt(yesterdayMonth) != 10) {
            dateYesterday = yesterdayMonth.replace("0", "") + yesterdayDay;
        } else {
            dateYesterday = yesterdayMonth + yesterdayDay;
        }
        int beforeYesterday = c.get(Calendar.DAY_OF_MONTH) - 1;
        c.set(Calendar.DAY_OF_MONTH, beforeYesterday);
        String beforeYesterdayMonth = dateFormat.format(c.getTime()).substring(5, 7);
        String beforeYesterdayDay = dateFormat.format(c.getTime()).substring(7, 10);
        String dateBeforeYesterday;
        if (Integer.parseInt(beforeYesterdayMonth) != 10) {
            dateBeforeYesterday = beforeYesterdayMonth.replace("0", "") + beforeYesterdayDay;
        } else {
            dateBeforeYesterday = beforeYesterdayMonth + beforeYesterdayDay;
        }
        date.add("今天");
        date.add(dateYesterday);
        date.add(dateBeforeYesterday);
        return date;
    }

}
